/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

jQuery(document).ready(
    function(){
        var open = false;
        var oldFigure = "";
        $(".incomeFigures").live("dblclick", function(){
            if(!open){
                open = true;
                oldFigure = $(this).text();
                $(this).html("<input class=\"incomeInput\" id=\""+$(this).attr('id')+"\" type=\"text\" value =\""+$(this).text()+"\"/>");
                $(".incomeInput").focus();
            }
            else{
              $('.incomeInput').blur();
              open = false;
              oldFigure = "";
            }

        });

        $(".incomeInput").livequery(function(){
            $(this).keypress(function(e){
               if(e.which == "13"){
                   $(this).blur();
               }
               else if(e.which == 0){
                   //alert("youhuo");
                   //location.reload(true);
                   $(this).val(oldFigure);
                   $(this).blur();
               }
               else{
                 //  alert(e.which);
               }
            });
            $(this).blur(function(){
                open = false;

                var href = $(".urlUpdateIncome").attr("href");
                var id = $("#projectId").val();
                var type = $(this).attr('id');
                var val = $(this).val();
                val = validNumber(val);
                if(oldFigure != val ){
                    $(this).parent().text(val);
                     $.ajax({
                            url: href,
                            data: "id="+id+"&typeOfIncome="+type+"&figure="+val,
                            cache: false,
                            async: true,
                            success: function(html){
                               //$("body").text(html);
                               location.reload(true);
                            }
                     });
                }
                else{
                    $(this).parent().text(oldFigure);
                }
                oldFigure = "";
            })
        });

        $(".editableFigures").live("dblclick", function(){ // The the figures that are written on the page
            if(!open){
                open = true;
                 oldFigure = $(this).text();
                $(this).html("<input class=\"figureInput\" id=\""+$(this).attr('id')+"\" type=\"text\" value =\""+$(this).text()+"\"/>");
                $(".figureInput").focus();
            }
            else{
              $('.figureInput').blur();
              open = false;
              oldFigure = "";
            }

        });

        $(".figureInput").livequery(function(){ // The input that will replace the old one
             $(this).keypress(function(e){
               if(e.which == "13"){
                   $(this).blur();
               }
               else if(e.which == 0){
                   //alert("youhuo");
                   //location.reload(true);
                   $(this).val(oldFigure);
                   $(this).blur();
               }
               else{
                 //  alert(e.which);
               }
            });
            $(this).blur(function(){
                open = false;
                var href = $(".urlUpdate").attr("href");
                var id = $("#employeeId").val();    // gets the employeeInstance.Id from bottom of Show
                var type = $(this).attr('id');
                var val = $(this).val();
                val = validNumber(val);
                $(this).parent().text(val);
                if(oldFigure != val ){
                 $.ajax({
                        url: href,
                        data: "id="+id+"&type="+type+"&figure="+val,
                        cache: false,
                        async: true,
                        success: function(html){
                            location.reload(true)
                        }
                 });
                }
                oldFigure = "";
            })
        });

        $(".variableCostFigures").live("dblclick", function(){
            if(!open){
                open = true;
                 oldFigure = $(this).text();
                $(this).html("<input class=\"variableCostInput\" id=\""+$(this).attr('id')+"\" type=\"text\" value =\""+$(this).text()+"\"/>");
                $(".variableCostInput").focus();
            }
            else{
              $('.variableCostInput').blur();
              open = false;
              oldFigure = "";
            }

        });

        $(".variableCostInput").livequery(function(){
            $(this).keypress(function(e){
               if(e.which == "13"){
                   $(this).blur();
               }
               else if(e.which == 0){
                   //alert("youhuo");
                   //location.reload(true);
                   $(this).val(oldFigure);
                   $(this).blur();
               }
               else{
                 //  alert(e.which);
               }
            });
            $(this).blur(function(){
                open = false;
                var href = $(".urlUpdateVariableCost").attr("href");
                var id = $("#projectId").val();
                var type = $(this).attr('id');
                var val = $(this).val();
                val = validNumber(val);
               
                if(oldFigure != val ){
                     $(this).parent().text(val);
                     $.ajax({
                            url: href,
                            data: "id="+id+"&idVariableCost="+type+"&figure="+val,
                            cache: false,
                            async: true,
                            success: function(html){
                                location.reload(true);
                            }
                     });
                }
                else{
                     $(this).parent().text(oldFigure);
                }
                oldFigure = "";
            })
        });

        $(".expenditureFigures").live("dblclick", function(){
            if(!open){
                open = true;
                 oldFigure = $(this).text();
                $(this).html("<input class=\"expenditureInput\" id=\""+$(this).attr('id')+"\" type=\"text\" value =\""+$(this).text()+"\"/>");
                $(".expenditureInput").focus();
            }
            else{
              $('.expenditureInput').blur();
              open = false;
              oldFigure = "";
            }

        });

        $(".expenditureInput").livequery(function(){
            $(this).keypress(function(e){
               if(e.which == "13"){
                   $(this).blur();
               }
               else if(e.which == 0){
                   //alert("youhuo");
                   //location.reload(true);
                   $(this).val(oldFigure);
                   $(this).blur();
               }
               else{
                 //  alert(e.which);
               }
            });
            $(this).blur(function(){
                open = false;
                var href = $(".urlUpdate").attr("href");
                var id = $("#projectId").val();
                var type = $(this).attr('id');
                var val = $(this).val();
                val = validNumber(val);
                if(oldFigure != val ){
                    $(this).parent().text(val);
                     $.ajax({
                            url: href,
                            data: "id="+id+"&type="+type+"&figure="+val,
                            cache: false,
                            async: true,
                            success: function(html){
                                location.reload(true);
                            }
                     });
                }
                else{
                    $(this).parent().text(oldFigure);
                }
                oldFigure = "";
            })
        });
        whoaId = "";
        $(".hoursFigures").live("dblclick", function(){
            if(!open){
                open = true;
                oldFigure = $(this).text();
                whoaId = $(this).attr('title');
                $(this).html("<input class=\"hoursInput\" id=\""+$(this).attr('id')+"\" type=\"text\" value =\""+$(this).text()+"\"/>");
                $(".hoursInput").focus();
            }
            else{
              $('.hoursInput').blur();
              open = false;
              oldFigure = "";
              whoaId = "";
            }

        });

        $(".hoursInput").livequery(function(){
            $(this).keypress(function(e){
               if(e.which == "13"){
                   $(this).blur();
               }
               else if(e.which == 0){
                   //alert("youhuo");
                   //location.reload(true);
                   $(this).val(oldFigure);
                   $(this).blur();
               }
               else{
                 //  alert(e.which);
               }
            });
            $(this).blur(function(){
                open = false;
                var href = $(".urlUpdateHours").attr("href");
                var type = $(this).attr('id');
                var val = $(this).val();
                val = validNumber(val);
                if(oldFigure != val ){
                    $(this).parent().text(val);
                     $.ajax({
                            url: href,
                            data: "whoaId="+whoaId+"&type="+type+"&figure="+val,
                            cache: false,
                            async: true,
                            success: function(html){
                               //$("body").text(html);
                               location.reload(true);
                            }
                     });
                }
                $(this).parent().text(oldFigure);
                oldFigure = "";
                whoaId = "";
            })
        });
        
        $(".editableFiguresArrow").live("click", function(){ // The the figures that are written on the page
            var href = $(".urlArrow").attr("href");
            var id = $("#employeeId").val();    // gets the employeeInstance.Id from bottom of Show
            var type = $(this).attr('id');
            var val = 0   //for input
             $.ajax({
                    url: href,
                    data: "id="+id+"&type="+type+"&figure="+val,
                    cache: false,
                    async: true,
                    success: function(html){
                        location.reload(true)
                    }
             });
        });

        function validNumber(a){
           var b = parseFloat(a);
           if(isNaN(b)){
               return oldFigure;
           }
           else return b;
        }
    }
);



